package com.hk.core.jdbc.tenants;

import com.hk.commons.util.CollectionUtils;
import com.hk.commons.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kevin
 * @date 2024-03-27 21:49
 */
public class CompositeTenantIdResolver {

    private final List<TenantIdResolver> resolvers = new ArrayList<>();

    public void registerResolvers(List<TenantIdResolver> resolvers) {
        this.resolvers.addAll(resolvers);
    }

    public String resolve(String dataSourceName) {
        if (CollectionUtils.isNotEmpty(resolvers)) {
            for (TenantIdResolver resolver : resolvers) {
                String resolve = resolver.resolve(dataSourceName);
                if (StringUtils.hasLength(resolve)) {
                    return resolve;
                }
            }
        }
        return dataSourceName;
    }
}
